package com.yq.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.entity.Accessrecords;
import com.yq.service.AccessrecordsService;
import com.yq.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;
import java.util.Date;
import java.util.*;

/**
 * @author zxn
 * @date 2020-03-16
 * @description 控制器
 */
@Api(value = "", tags = "")
@RestController
@RequestMapping("accessrecords")
public class AccessrecordsController {

    @Autowired
    private AccessrecordsService targetService;

    /**
     * 获取分页数据
     *
     * @return
     */
    @ApiOperation(value = "获取分页列表", notes = "获取分页列表")
    @GetMapping(value = "/list")
    public R list(@RequestParam(required = false) Map map) {
        int current = Integer.parseInt(map.get("current").toString());
        int size = Integer.parseInt(map.get("size").toString());
        Page page= new Page(current, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        map.forEach((key, value) -> {
            if (!"current".equals(key) && !"size".equals(key)){
                queryWrapper.like(key,value);
            }
        });
        return R.ok().put("data",targetService.pageMaps(page,queryWrapper));
    }

    /**
     * 根据ID查找数据
     */
    @ApiOperation(value = "获取详情", notes = "根据ID获取详情")
    @GetMapping("/get")
    public R get(@RequestParam("id") Long id) {
        Accessrecords entity = targetService.getById(id);
        List<Accessrecords> ents = new ArrayList<>();
        ents.add(entity);
        return R.ok().put("data",ents);
    }

    /**
     * 添加数据
     * @return
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    public R add(@RequestBody Accessrecords entity) {
        targetService.save(entity);
        return R.ok();
    }

    /**
     * 更新数据
     * @return
     */
    @ApiOperation(value = "编辑", notes = "编辑")
    @PostMapping("/update")
    public R update(@RequestBody Accessrecords entity) {
        targetService.updateById(entity);
        return R.ok();
    }

    /**
    * 删除数据
    * @return
    */
    @ApiOperation(value = "移除", notes = "移除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", required = true, value = "id", paramType = "form")
    })
    @PostMapping("/remove")
    public R remove(@RequestParam(value = "id") Long id) {
        targetService.removeById(id);
        return R.ok();
    }

    /**
    * 批量删除数据
    * @return
    */
    @ApiOperation(value = "批量移除", notes = "批量移除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "ids", required = true, value = "多个用,号隔开", paramType = "form")
    })
    @PostMapping("/batch/remove")
    public R batchRemove(@RequestParam(value = "ids") String ids) {
        targetService.removeByIds(Arrays.asList(ids.split(",")));
        return R.ok();
    }
}
